﻿/*
	COPYRIGHT © 2007 - 2008 Ulrik S. Andersson.
	
	This file is part of MOM.

    MOM is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

    MOM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along with MOM.  If not, see <http://www.gnu.org/licenses/>.
*/

function LoadCompleteMetaDataForArtist(artist)
{
	var originalArtist = artist;
	if (artist.indexOf(", The") > -1)
		artist = "The " + artist.substr(0, artist.length - 5);
	artist = artist.replace(" & ", " and ");
	
	GetWikiArtistInfo(artist, originalArtist);
	GetLastFmRelatedArtists(artist);
	GetLastFmTopAlbums(artist);	
	GetFlickrForArtist(artist);
}

/* ------------------------ WIKIPEDIA ----------------------------- */
function GetWikiArtistInfo(artist, originalArtist)
{
	$.getJSON("DB/JSON/Wikipedia.aspx", {a:"search",q:artist}, function (json) { Wiki_Callback(json, artist, originalArtist); });
}

function Wiki_Callback(json, artist, originalArtist)
{
	var record = null;
	//If any of the hits contains band, artist, musician or entertainer grab it
	for (var i = 0; i < json.length; i++)
	{		
		var title = json[i].toLowerCase();
		if (title.indexOf(artist.toLowerCase()) > -1 && (title.indexOf("band") > -1 || title.indexOf("artist") > -1 || title.indexOf("musician") > -1 || title.indexOf("entertainer") > -1 || title.indexOf("singer") > -1 || title.indexOf("producer") > -1))
		{
			record = json[i];
			break;
		}		
	}
	
	if (record == null)
	{
		//Check for perfect title match
		for (var i = 0; i < json.length; i++)
		{		
			if (json[i].toLowerCase() == artist.toLowerCase())
			{
				record = json[i];
			}
		}
	}
	
	//Consider this a "no hit"
	if (record == null)
	{
		$("#ArtistWiki").createAppend('span', {}, window.Translate.WikipediaNoHits);
		return;
	}
	
	$.getJSON("DB/JSON/Wikipedia.aspx", {ar:originalArtist,q:record}, function (json) { WikiContent_Callback(json, "http://en.wikipedia.org/wiki/" + record); });
}

function WikiContent_Callback(json, url)
{
	$("#ArtistWiki").createAppend('span', {}, json.Content).createAppend('a', {href:url,target:'_blank',className:'ReadMore'}, window.Translate.WikipediaReadMore);
}

/* ------------------------ /WIKIPEDIA ----------------------------- */

/* ------------------------ LAST.FM ----------------------------- */
function GetLastFmRelatedArtists(artist)
{
	$.getJSON("DB/JSON/LastFm.aspx", {ac:"similar",a:artist}, GetLastFmRelatedArtists_Callback);
}

function GetLastFmRelatedArtists_Callback(json)
{
	var el = $("#ArtistSimilar");
	for (var i = 0; i < json.Result.length; i++)
	{
		el.createAppend('a', {href:json.Result[i].Url,target:"_blank"}, json.Result[i].Artist);
	}
	
	$.getJSON("DB/JSON/Browse.aspx", {t:"artist"}, function(result) {BrowseArtist_Callback(result, json.Result);});
}

function BrowseArtist_Callback(json, lastFmArtists)
{
	var lastFmLinks = $("#ArtistSimilar > a");
	for (var i = 0; i < lastFmArtists.length; i++)
	{
		for (var j = 0 ; j < json.result.length; j++)
		{
			var lastFmArtist = lastFmArtists[i].Artist.toLowerCase();
			var localArtist = json.result[j].name.toLowerCase();
			if (localArtist.indexOf(", the") > -1)
				localArtist = "the " + localArtist.substr(0, localArtist.length - 5);
			localArtist = localArtist.replace("&", " and ");
			if (lastFmArtist == localArtist)
			{
				var link = lastFmLinks.eq(i);
				link.text(link.text() + "*");
				link.attr({href:"Browse.aspx?t=album&id=" + json.result[j].id,target:"_self"});
				break;
			}
		}
	}
	$("#ArtistSimilar").createAppend('span', {className:'TxtSmall'}, window.Translate.ArtistIsInLibrary);
}

function GetLastFmTopAlbums(artist)
{
	$.getJSON("DB/JSON/LastFm.aspx", {ac:"topalbums",a:artist}, GetLastFmTopAlbums_Callback);
}

function GetLastFmTopAlbums_Callback(json)
{
	var el = $("#ArtistTopAlbums");
	for (var i = 0; i < json.Result.length; i++)
	{
		el.createAppend('span', {}, json.Result[i].Album);
	}
}
/* ------------------------ /LAST.FM ----------------------------- */

/* ------------------------ FLICKR ----------------------------- */
function GetFlickrForArtist(artist)
{
	artist = artist.replace("The ", "");
	$.getJSON("DB/JSON/Flickr.aspx", {a:artist}, GetFlickrForArtist_Callback);
}

function GetFlickrForArtist_Callback(json)
{
	var el = $("#ArtistFlickr");
	for (var i = 0; i < json.Result.length; i++)
	{
		el.createAppend('a', {href:'javascript:void(0);',title:json.Result[i].Title}, [
			'img', {src:json.Result[i].ThumbUrl,alt:json.Result[i].Title}, ''
		]).bind("click", {Url:json.Result[i].LargeUrl,Web:json.Result[i].WebUrl,Title:json.Result[i].Title}, ViewFlickrImage);
	}
}

function ViewFlickrImage(e)
{
	var drop = window.top.document.getElementById("Backdrop");
	$(drop).empty();
	$(drop).html('<div class="FlickrView"><img src="' + e.data.Url + '" /><a href="' + e.data.Web + '" target="_blank">' + e.data.Title + '</a></div>');
	$(drop).css("display", "block");
}
/* ------------------------ /FLICKR ----------------------------- */